package com.bluemsun.naturallanguageprocessing.controller;

import com.alibaba.fastjson.JSONObject;
import com.bluemsun.naturallanguageprocessing.common.ResponseCode;
import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.entity.User;
import com.bluemsun.naturallanguageprocessing.service.IUserService;
import com.bluemsun.naturallanguageprocessing.util.JsonUtil;
import com.bluemsun.naturallanguageprocessing.util.RedisUtil;
import org.apache.catalina.Server;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    RedisUtil redisUtil;
    @Resource
    IUserService userService;

    @PostMapping("/login")
    @ResponseBody
    public ServerResponse<Object> login(@RequestBody User user, HttpSession session, HttpServletRequest request){
        if(userService.checkUser(user).isSuccess()) {
            ServerResponse<Object> response = userService.checkUser(user);
            session.setAttribute("user", userService.checkUser(user).getData());
//            String userInfo = UUID.randomUUID().toString();
//            redisUtil.setEx(userInfo, JsonUtil.obj2String(response.getData()),60*30);
            return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getDesc());
        }
        return ServerResponse.createByErrorMessage("登陆失败，用户名或者密码错误");
    }
    @PostMapping("/register")
    @ResponseBody
    public ServerResponse<Object> register(@RequestBody User user){
        return userService.registerUser(user);
    }
    /**
     * @author     ：hj
     * @date       ：Created in 2020/11/21 23:53
     * @description：更新用户信息
     * @modified By：
     * @version:
     */
//    @PostMapping("/update")
//    @ResponseBody
//    public ServerResponse<Object> update(@RequestBody User user){
//
//        return userService.updateUser(user);
//    }
    /**
     * @author     ：hj
     * @date       ：Created in 2020/11/21 23:53
     * @description：退出登录
     * @modified By：
     * @version:
     */
    @GetMapping("/logout")
    @ResponseBody
    public ServerResponse<Object> logout(HttpSession session){
        session.removeAttribute("user");
        return ServerResponse.createBySuccessMessage("退出登录成功");
    }
    /**
     * @author     ：hj
     * @date       ：Created in 2020/11/21 23:52
     * @description：分页模糊搜索用户
     * @modified By：
     * @version:
     */
//    @GetMapping("/selectUser")
//    @ResponseBody
//    public ServerResponse<Object> selectUser(@RequestParam("username") String username,@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNum") Integer pageNum){
//        return userService.selectUser(username,pageSize,pageNum);
//    }
//    /**
//     * @author     ：hj
//     * @date       ：Created in 2020/11/21 23:52
//     * @description：查询所有用户
//     * @modified By：
//     * @version:
//     */
//    @GetMapping("/selectAllUser")
//    @ResponseBody
//    public ServerResponse<Object> selectALlUser(@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNum") Integer pageNum){
//        return userService.selectAllUser(pageSize,pageNum);
//    }
}
